package com.mreko.chatroot;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.mreko.pojo.ChatBean;
import com.mreko.util.ChatThread;

public class ChatActivity extends ActionBarActivity {
	private String ip = "172.23.204.231";
	private int port = 25555;
	private Handler handler;
	private ListView listView;
	private List<ChatBean> chatBeans = new ArrayList<ChatBean>();
	private Button sendButton;
	private EditText editText;

	// 后台处理聊天信息的Thread5
	private ChatThread chatThread;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat);
		listView = (ListView) findViewById(R.id.chatList);
		sendButton = (Button) findViewById(R.id.ib_send);
		editText = (EditText) findViewById(R.id.et_input);
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// 来自子线程的消息
				//用于更新UI界面
				if (msg.what == 0x123) {
					ChatBean chatBean = (ChatBean) msg.obj;
					chatBeans.add(chatBean);
					ChatAdapter chatAdapter = new ChatAdapter(chatBeans,
							ChatActivity.this);
					listView.setAdapter(chatAdapter);
					listView.setSelection(listView.getBottom());
				}
			}
		};
		ChatAdapter chatAdapter = new ChatAdapter(chatBeans, this);
		listView.setAdapter(chatAdapter);
		chatThread = new ChatThread(handler, ip, port, this);
		System.out.println("create chatThread");
		new Thread(chatThread).start();
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ChatBean chatBean = new ChatBean();
				chatBean.setContent(editText.getText().toString());
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				Date date = new Date();
				chatBean.setTime(df.format(date));
				Message msg=new Message();
				msg.obj=chatBean;
				chatThread.sendHandler.sendMessage(msg);
				editText.setText("");
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chat, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
